<template>
	<view class="bg-light flex flex-column">
		<radio-group>
			<view style="position: relative" @click.stop="handle(index)" class="mx-3 mt-3 p-3 bg-white rounded-lg"
				v-for="(item, index) in Pickup" :key="index">
				<view class="border-bottom border-light">
					<text class="font font-weight-bold" style="display: block">{{
            item.Pickup
          }}</text>
					<text class="font py-2" style="display: block">{{ item.Name }}&nbsp;{{ item.Phone }}</text>
				</view>
				<view class="mt-3 flex align-center justify-between">
					<!-- <label @click.stop="select(index)">
						<radio color="#17a2b8" style="transform:scale(0.6);" :checked="item.Default_Type" />
						<text class="font">默认地址</text>
					</label> -->
					<image @click.stop="Edit(item.Picker_SerialNumber)" :src="Domain_ICON + '/edit.png'"
						style="width: 40rpx; height: 40rpx" mode=""></image>
				</view>
				<view v-if="isdelete" @click.stop="DeletePickup(item)"
					class="bg-danger text-white font-lg flex justify-center" style="
            line-height: 45rpx;
            position: absolute;
            right: 0;
            top: 0;
            border-radius: 50%;
            width: 50rpx;
            height: 50rpx;
          ">
					×
				</view>
			</view>
		</radio-group>
		<view class="font-sm py-2 text-center">
			<text>{{ DropdownRow_Status }}</text>
		</view>
		<view :style="'height:' + fixedHeight + 'px'"> </view>
		<view id="fixed" class="px-4 py-2 fixed-bottom bg-white flex">
			<view class="w-100 bg-info rounded text-center py-2 mr-2 text-white font-md" @click="Add()"
				hover-class="bg-hover-info" :hover-stay-time="150">新增提货人</view>
			<view class="w-100 rounded text-center py-2 text-white font-md" :class="buttonType" @click="SelectDelete">
				{{ buttonText }}</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				isdelete: false,
				onReach: true,
				fixedHeight: 0,
				Pickup: [], //地址列表
				Path: null, //回调路径
				loading: true,
				buttonText: "删除地址",
				buttonType: "bg-danger",
				form: {
					page: 1,
					limit: 10,
					App_ID: app.globalData.appid,
					Open_ID: app.globalData.openid,
				},
				DropdownRow_Status: "下拉查看更多",
				total: 0,
				Domain_ICON: app.globalData.Domain_ICON, //全局变量域名
			};
		},
		onShow() {
			this.getList(this.form);
		},
		onLoad(options) {
			if (options.Path) {
				//有无路径
				this.Path = options.Path;
			}
			this.fixed();
		},
		methods: {
			fixed() {
				//底部模块高度获取
				const query = uni.createSelectorQuery();
				query
					.select("#fixed")
					.boundingClientRect((data) => {
						this.fixedHeight = data.height;
					})
					.exec();
			},
			getList(data, onReach = false) {
				uni.showLoading({
					title: "查询中",
				});
				this.DropdownRow_Status = "玩命加载中...";
				uni.request({
					//查询地址列表
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
					},
					url: app.globalData.Domain_Name + "/getPickerAllAction?grant_type=wechat_program", //销售价格单
					method: "POST",
					data,
					success: ({
						data
					}) => {
						// if (data.tableData.length == 0) {
						//   this.Add();
						// }
						if (!onReach) {
							this.Pickup = [];
						}
						this.Pickup.push(...data.tableData);
						this.total = parseInt(data.limit);
						if (this.total > 10) {
							this.DropdownRow_Status = "下拉查看更多";
						} else {
							this.DropdownRow_Status = "没有更多了";
						}
						let Pickup = uni.getStorageSync("PickupData");
						this.Pickup.forEach((item) => {
							item.Default_Type = false;
						});
						if (
							this.Pickup.some(
								(item) =>
								item.Picker_SerialNumber == Pickup.Picker_SerialNumber
							)
						) {
							this.Pickup.filter(
								(item) =>
								item.Picker_SerialNumber == Pickup.Picker_SerialNumber
							);
						}

						uni.hideLoading();
					},
				});
			},
			select(index) {
				//设置默认的地址
				if (!this.Pickup[index].Default_Type) {
					//点击的地址为非默认地址
					this.Pickup.forEach((item) => {
						item.Default_Type = false;
					});
					this.Pickup[index].Default_Type = true;
				} else {
					this.Pickup[index].Default_Type = false;
				}
			},
			handle(index) {
				//点击地址
				if (this.Path != null) {
					uni.setStorageSync("PickupData", this.Pickup[index]);
					uni.navigateBack();
				}
			},
			Add() {
				//跳转新建地址
				uni.navigateTo({
					url: "/pages/my/pickupPersonAdd",
				});
			},
			Edit(id) {
				//跳转修改地址
				uni.navigateTo({
					url: "/pages/my/pickupPersonEdit?Picker_SerialNumber=" + id,
				});
			},
			SelectDelete() {
				this.isdelete = !this.isdelete;
				if (this.isdelete) {
					(this.buttonText = "完成"), (this.buttonType = "bg-success");
				} else {
					(this.buttonText = "删除地址"), (this.buttonType = "bg-danger");
				}
			},
			DeletePickup(item) {
				const this_ = this;
				uni.showModal({
					title: "提示",
					content: "删除该地址后将无法恢复!",
					confirmText: "删除",
					success(res) {
						if (res.cancel) {
							uni.showToast({
								title: "已取消",
								icon: "none",
							});
						}
						if (res.confirm) {
							// 删除地址
							console.log(item);
							item.Row_Status = "1";
							uni.showLoading({
								title: "删除中...",
							});
							// this_.loading = true
							this_.Edit1(item);
						}
					},
				});
			},
			Edit1(item) {
				uni.request({
					//查询地址列表
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
					},
					url: app.globalData.Domain_Name + "/WXEditAction", //销售价格单
					method: "POST",
					data: {
						Data: JSON.stringify({
							Modify: {
								member_picker_info: [item],
							},
						}),
						Open_ID: app.globalData.openid,
					},
					success: (res) => {
						uni.hideLoading();
						if (res.data.Status == "success") {
							uni.showToast({
								title: "删除成功!",
								icon: "success",
							});
							this.Pickup = [];
							this.form.page = 1;
							this.getList(this.form);
						}
					},
				});
			},
		},
		onReachBottom() {
			if (this.onReach) {
				if (this.Pickup.length < this.total) {
					this.form.page++;
					this.getList(this.form, true);
				} else {
					this.onReach = false;
					this.DropdownRow_Status = "没有更多了";
				}
			}
		},
	};
</script>

<style scoped>
	page {
		background-color: #f8f9fa;
	}
</style>